home *** CD-ROM | disk | FTP | other *** search
- AECDEV/AEDaemon
- This sample is designed to answer that ago-old question;
- "How do I send AppleEvents from a DA/CDEV/INIT/Driver?"
- The short answer is, you can't. The High Level Event manager, which the
- AppleEvent manager uses to dispatch AppleEvents, will not allow a
- PostHighLevel event call from CODE that does not have a PPC port and
- event loop to recieve the reply back through. DA/CDEV/INIT code
- does not have an event loop, so they cannot be high level event aware,
- ergo they cannot send AppleEvents. Bad situation.
-
- The Solution
- The solution is to have a helper do it for you.
- This means that instead of one bit of code, you have two. The
- CDEV (in this case) prepares all the data that will be a part of
- the AppleEvent, then passes the data to a faceless background application.
- The faceless background application, since it is a 'real' app with an
- event loop, can send the AppleEvent.
-
- How It Works
- When you click the 'Send an odoc event' button in the CDEV, the CDEV
- builds an 'odoc' AppleEvent, using AppleEvent Manager calls.
- Then it looks for the backgrounder. It first does an IPCListPorts to
- see if the backgrounder's PPC port is currently open. If it is not,
- it searches (using PBCatSearch) for the backgrounder, and launches it
- when it finds it, and keeps trying IPCListPorts until the port opens.
- When the CDEV has found the backgrounder, it opens a PPC port for itself, then
- begins a PPC session with the backgrounder. The CDEV writes the data
- portion of the AppleEvent it has created to the backgrounder.
- The backgrounder reads all the data, when the reading is done, it sends
- the AppleEvent using AESend.
- Simple.
-
- And this sample is simple, all it does is send a reply-less odoc event.
- But you can easily build it up to do more.
- Currently, the only data that is sent across is the 'odoc' event data. But,
- you can also transfer a 'header' block before the MAVT data block, which could
- specify what it is you're sending, if you want a reply, the priority, the interaction
- level, or anything else. An example would be...
-
- struct MyAEvtPPCHeader{
- short Interaction;
- short reply;
- Boolean layerSwitch;
- short priority;
- short timeOut;
- };
-
- Currently, the AECDEV closes it's port after it sends the data. If you wanted a
- reply, all you'd do would be to add a PPCRead after the PPCWrite, and the AEDaemon
- would wait for the reply, then send the reply back with a PPCWrite.
- Again, I wanted to keep this simple, so I didn't add that section. But all the
- calls to do it are already here, just re-arrange them to suit your needs.
-
- Launching AEDaemon
- Every time the AECDEV wants to send an event, it searches for the AEDaemon and
- launches it if it can't find it. This continual launching could cause
- fragmentation of MultiFinder memory, so we recommend that AEDaemon (or whatever
- you write) be installed in the StartUp items folder in the system folder. This way
- it will be launched every time the user reboots.
- Also, by launching right after the Finder starts, it will be high in MultiFinder's
- heap, so it will not be causing any fragmentation problems.
-
- I hope this sample helps you folks who are working with DAs and other non-application
- code under system 7.
- However, I also strongly recommend that you convert your DAs or CDEVs to small
- applications instead. Using a helper application is a stopgap, and not the
- most efficient answer. When you can safely assume that you will be running in
- a System 7 world, the benifits of converting to a Application should make it
- compelling for you to do so.
- Applications have many more rights and priledges than other CODE bits, and with
- full-time MultiFInder you should be able to make the conversion, and live a
- happier life.
- C.K. Haun
- Apple DTS